{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Knapsack Problem\n",
    "\n",
    "Read more about it from here: https://developers.google.com/optimization/bin/knapsack\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Solver\n",
    "\n",
    "We gonna use WOA to solve this problem\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: Solving single objective optimization problem.\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 1, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00374 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 2, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00434 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 3, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00327 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 4, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00383 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 5, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00368 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 6, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00397 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 7, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00383 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 8, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00327 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 9, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00426 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 10, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00429 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 11, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00355 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 12, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00389 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 13, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00346 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 14, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00394 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 15, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00433 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 16, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00376 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 17, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00399 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 18, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00319 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 19, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00373 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 20, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00333 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 21, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00357 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 22, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00364 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 23, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00524 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 24, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00377 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 25, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00408 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 26, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00326 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 27, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00368 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 28, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00530 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 29, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00428 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 30, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00375 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 31, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00390 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 32, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00501 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 33, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00477 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 34, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00442 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 35, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00427 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 36, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00387 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 37, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00409 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 38, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00413 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 39, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00365 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 40, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00485 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 41, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00348 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 42, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00371 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 43, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00381 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 44, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00451 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 45, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00343 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 46, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00368 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 47, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00405 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 48, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00405 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 49, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00349 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 50, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00346 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 51, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00363 seconds\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 52, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00490 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 53, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00325 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 54, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00406 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 55, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00393 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 56, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00293 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 57, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00351 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 58, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00386 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 59, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00329 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 60, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00421 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 61, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00414 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 62, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00331 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 63, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00382 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 64, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00431 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 65, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00359 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 66, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00384 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 67, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00335 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 68, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00443 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 69, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00438 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 70, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00385 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 71, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00338 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 72, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00402 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 73, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00378 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 74, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00376 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 75, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00415 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 76, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00329 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 77, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00404 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 78, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00415 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 79, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00363 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 80, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00378 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 81, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00418 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 82, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00347 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 83, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00366 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 84, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00398 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 85, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00321 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 86, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00405 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 87, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00366 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 88, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00463 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 89, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00384 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 90, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00361 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 91, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00479 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 92, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00295 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 93, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00434 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 94, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00321 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 95, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00427 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 96, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00314 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 97, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00395 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 98, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00386 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 99, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00387 seconds\n",
      "2023/10/31 05:17:40 PM, INFO, mealpy.swarm_based.WOA.OriginalWOA: >>>Problem: P, Epoch: 100, Current best: 5026.0, Global best: 5026.0, Runtime: 0.00385 seconds\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id: 6236, target: Objectives: [5026], Fitness: 5026.0, solution: [1 1 0 0 0 1 1 0 1 0 1 1 1 0 1 1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 0 1 0 0 0 0 1\n",
      " 0 0 0 0 1 0 0 1 0 0 0 1 1]\n",
      "Best fitness: 5026.0, Best solution: {'bin': array([1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0,\n",
      "       1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,\n",
      "       1, 0, 0, 0, 1, 1])}\n"
     ]
    }
   ],
   "source": [
    "from mealpy import BinaryVar, Problem, WOA\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "class KP(Problem):\n",
    "    def __init__(self, bounds=None, minmax=\"min\", name=\"KP\", data=None, **kwargs):\n",
    "        self.name = name\n",
    "        self.data = data\n",
    "        super().__init__(bounds, minmax, **kwargs)\n",
    "    \n",
    "    def constrains(self, capacity):\n",
    "        return capacity - self.data[\"capacity\"]\n",
    "\n",
    "    def obj_func(self, solution):\n",
    "        solution = self.decode_solution(solution)\n",
    "        x = solution[\"bin\"]\n",
    "        current_capacity = np.sum(x * self.data[\"weights\"])\n",
    "        violation = max(0, self.constrains(current_capacity))\n",
    "        value = np.sum(x * self.data[\"values\"])\n",
    "        return value - violation**2\n",
    "\n",
    "data = {\n",
    "    \"values\": np.array([\n",
    "        360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48, 147,\n",
    "        78, 256, 63, 17, 120, 164, 432, 35, 92, 110, 22, 42, 50, 323, 514, 28,\n",
    "        87, 73, 78, 15, 26, 78, 210, 36, 85, 189, 274, 43, 33, 10, 19, 389, 276,\n",
    "        312 ]),\n",
    "    \"weights\": np.array([\n",
    "            7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9, 0,\n",
    "            42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71,\n",
    "            3, 86, 66, 31, 65, 0, 79, 20, 65, 52, 13 ]),\n",
    "    \"capacity\": 850\n",
    "}\n",
    "\n",
    "    \n",
    "## Now, we define an algorithm\n",
    "problem = KP(bounds=BinaryVar(n_vars=50, name=\"bin\"), minmax=\"max\", name=\"KP\", data=data)\n",
    "model = WOA.OriginalWOA(epoch=100, pop_size=30)\n",
    "g_best = model.solve(problem)\n",
    "print(g_best)\n",
    "print(f\"Best fitness: {g_best.target.fitness}, Best solution: {model.problem.decode_solution(g_best.solution)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}